package elevatronic.view.simulation;

import java.util.ArrayList;
import java.util.List;

import elevatronic.models.Floor;
import elevatronic.models.People;
import elevatronic.utils.Utils;
import elevatronic.view.ImageView;
import elevatronic.view.MainFrame;

/**
 * 
 * @author Benjamin CORNEC
 * floor view of floor model
 */
public class FloorView {
	
	public static final int POS_X=40;
	public static final int POS_X_CENTER=370;
	public static final int FIRST_FLOOR_POS_Y=244;
	public static final int SEPARATION_BETWEEN_TWO_FLOORS=50;
	
	//floor model
	private Floor floor;
	public Floor getFloor() {
		return floor;
	}
	public void setFloor(Floor floor) {
		this.floor = floor;
	}
	
	
	//contain all people on the floor
	private List<PeopleView> peopleView;
	public List<PeopleView> getPeopleView() {
		return peopleView;
	}
	public void setPeopleView(List<PeopleView> peopleView) {
		this.peopleView = peopleView;
	}
	
	
	//get position of the frame
	public int getStep(){
		return MainFrame.HEIGHT-getImageFloor().getHght();
	}
	
	
	//image of the floor
	private ImageView imageFloor;
	public ImageView getImageFloor() {
		return imageFloor;
	}
	public void setImageFloor(ImageView imageFloor) {
		this.imageFloor = imageFloor;
	}
	
	//constructor
	public FloorView(Floor floor){
		setFloor(floor);
		setImageFloor(new ImageView("floor",0,0,732,49));
		setPeopleView(new ArrayList<PeopleView>());
		for (People people:floor.getPeopleAtFloor()){
			getPeopleView().add(new PeopleView(people));
		}
		setLocalisation();
	}
	
	/**
	 * This method allow to set X and Y on the screen
	 */
	private void setLocalisation(){
		getImageFloor().setPosX(POS_X);
		getImageFloor().setPosY(Utils.GetPosY(getFloor().getNumber(), getImageFloor().getHght()));

	}
	
	
	/**
	 * updatePeopleViewList is used to refresh the peopleView list on the floor, indeed, people go in and out everytime, we have to refresh who are in
	 */
	public void updatePeopleViewList(){
		List<PeopleView> morePeopleView = new ArrayList<PeopleView>();
		List<PeopleView> lessPeopleView = new ArrayList<PeopleView>();		
		//if people is not present anymore in simulation, we delete it in the view
		for(PeopleView peopleView:getPeopleView()){
			//<search the people>
			boolean found=getFloor().getPeopleAtFloor().contains(peopleView.getPeople());
			//</search the people>
			if (!found){
				lessPeopleView.add(peopleView);
			}
		}
		
		//if any people of the simulation is not present in the view, we add it
		for (People people:getFloor().getPeopleAtFloor()){
			//<search the people>
			boolean found=false;
			for(PeopleView peopleView:getPeopleView()){
				if (people==peopleView.getPeople()){
					found=true;
					break;
				}
			}
			//</search the people>
			if (!found){
				morePeopleView.add(new PeopleView(people));
			}			
		}
		
		//modify the list
		getPeopleView().removeAll(lessPeopleView);
		getPeopleView().addAll(morePeopleView);
		
		//people will get a position on the floor to wait the elevator
		int i=0;
		for (PeopleView peopleView:getPeopleView()){
			peopleView.setIdOnTheFloor(i);
			i++;
		}
	}
	
	//iteration of the floor
	public void update(int step){
		updatePeopleViewList();
		getImageFloor().setVisible(getFloor().getPeopleAtFloor().size()!=0);
	}
}
